﻿using System;
using System.Collections.Generic;

namespace Alex.Library.Log
{
    /// <summary>
    /// 控制台日志
    /// </summary>
    public class ConsoleLogger : LoggerBase
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ConsoleLogger()
        {
            InitForegroundColor();
        }

        private Dictionary<LogLevel, ConsoleColor> _ForegroundColor;
        /// <summary>
        /// 初始化控制台输出每种级别日志的前景色
        /// </summary>
        private void InitForegroundColor()
        {
            this._ForegroundColor = new Dictionary<LogLevel, ConsoleColor>();
            this._ForegroundColor[LogLevel.DEBUG] = ConsoleColor.White;
            this._ForegroundColor[LogLevel.INFO] = ConsoleColor.White;
            this._ForegroundColor[LogLevel.WARN] = ConsoleColor.Yellow;
            this._ForegroundColor[LogLevel.ERROR] = ConsoleColor.Green;
            this._ForegroundColor[LogLevel.FATAL] = ConsoleColor.Red;
        }

        /// <summary>
        /// 设置控制台前景色
        /// </summary>
        public void SetForegroundColor(LogLevel level, ConsoleColor color)
        {
            this._ForegroundColor[level] = color;
        }

        /// <summary>
        /// 获取控制台前景色
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public ConsoleColor GetForegroundColor(LogLevel level)
        {
            return this._ForegroundColor[level];
        }

        /// <summary>
        /// 将日志输出到控制台
        /// </summary>
        /// <param name="level">日志级别</param>
        /// <param name="message">消息</param>
        protected override void DoFlush(LogLevel level, string message)
        {            
            string content = this.Template.Format(level, message);
            Console.ForegroundColor = _ForegroundColor[level];
            Console.WriteLine(content);
        }
    }
}
